package com.pink.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pink.entity.config.AppConfig;
import com.pink.entity.config.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;

@Component
public class FFmpegUtils {

    @Resource
    private AppConfig appConfig;

    // 获取ffmpeg路径
    public String getFFmpegPath(){
        String ffmpegPath = appConfig.getFfmpegPath();
        return ffmpegPath;
    }

    // 创建视频缩略图
    public void createImageThumbnail(String filePath) {
        filePath = filePath.replace("/", appConfig.getSeparator());
        String CMD = getFFmpegPath() + "ffmpeg -i %s -vf scale=200:-1 %s";
        CMD = String.format(CMD, filePath, filePath+ Constants.IMAGE_THUMBNAIL_SUFFIX);
        ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
    }

    //获取文件类型
    public String getFileType(String filePath){
        filePath = filePath.replace("/", appConfig.getSeparator());
        String CMD = getFFmpegPath() + "ffprobe -v error -show_format -print_format json %s";
        CMD = String.format(CMD, filePath);
        String result = ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject format = jsonObject.getJSONObject("format");
        if(format == null){
            return "";
        }
        if(format.getString("format_name") == null){
            return "";
        }
        return format.getString("format_name");
       }

    // 获取视频时长
    public Integer getVideoDuration(String filePath) {
        filePath = filePath.replace("/", appConfig.getSeparator());
        final String CDM_GET_CODE = getFFmpegPath() +"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 %s";
        String CMD = String.format(CDM_GET_CODE, filePath);
        String result = ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
        if(StringTool.isEmpty(result)){
            return 0;
        }else{
            result = result.replace("\n","");
            return new BigDecimal(result).intValue();
        }
    }

    //获取视频分辨率
    public String getVideoResolution(String filePath) {
        filePath = filePath.replace("/", appConfig.getSeparator());
        final String CDM_GET_CODE = getFFmpegPath() +"ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 %s";
        String CMD = String.format(CDM_GET_CODE, filePath);
        String result = ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
        if(StringTool.isEmpty(result)){
            return "720P";
        }
        return result;
    }

    // 获取视频编码格式
    public String getVideoCodec(String filePath) {
        filePath = filePath.replace("/", appConfig.getSeparator());
        final String CDM_GET_CODE = getFFmpegPath() +"ffprobe -v error -select_streams v:0 -show_entries stream=codec_name %s";
        String CMD = String.format(CDM_GET_CODE, filePath);
        String result = ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
        result = result.replace("\n","");
        result = result.substring(result.indexOf("=") + 1);
        String codec = result.substring(0,result.indexOf("["));
        return codec;
    }

    // 将HEVC格式视频转换为MP4格式
    public void convertHevc2Mp4(String newFileName, String videoFilePath){
        newFileName = newFileName.replace("/", appConfig.getSeparator());
        videoFilePath = videoFilePath.replace("/", appConfig.getSeparator());
        final String CMD_HEVC_264 = getFFmpegPath() +"ffmpeg -i %s -c:v libx264 -crf 20 %s -y";
        String CMD = String.format(CMD_HEVC_264, newFileName, videoFilePath);
        ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
    }

    // 将视频转换为TS格式
    public void convertVideo2Ts(File tsFolder, String videoFilePath) {
        videoFilePath = videoFilePath.replace("/", appConfig.getSeparator());
        final String CMD_TRANSFER_2TS = getFFmpegPath() +"ffmpeg -y -i %s -vcodec copy -acodec copy -bsf:v h264_mp4toannexb %s";
        final String CMD_CUT_TS = getFFmpegPath() +"ffmpeg -i %s -c copy -map 0 -f segment -segment_list %s -segment_time 10 %s"+appConfig.getSeparator()+"%%4d.ts";
        String tsPath = tsFolder+appConfig.getSeparator()+Constants.TS_NAME;
        //生成TS
        String cmd = String.format(CMD_TRANSFER_2TS, videoFilePath, tsPath);
        ProcessUtils.executeCommand(cmd,appConfig.getShowFFmpegLog());
        //生成index.m3u8和TS
        cmd = String.format(CMD_CUT_TS,tsPath,tsFolder.getPath() +appConfig.getSeparator()+Constants.M3U8_NAME, tsFolder);
        ProcessUtils.executeCommand(cmd,appConfig.getShowFFmpegLog());
        //删除index.ts
        new File(tsPath).delete();
    }
}
